# -*- coding: utf-8 -*-

from flask import Flask, request, session, g, redirect, url_for, abort, \
     render_template, flash
import oursql


# create our little application :)
app = Flask(__name__)

#@app.route('/hello')
#@app.route('/hello/<name>')
#def hello(name='omneya'):
 #   return render_template('layout.html', name=name)


@app.route('/')
def registration():
        firstname = 'omneya'
        lastname = None
        return render_template('registration.html')

@app.route('/add', methods=['GET', 'POST'])
def add_entry():
    #insert into database
    firstname = str(request.form['firstname'])
    errorFN = None
    lastname= str(request.form['lastname'])
    errorLN = None
    month = str(request.form['month'])
    errorMonth = None
    day = str(request.form['day'])
    errorDay = None
    year = str(request.form['year'])
    errorYear = None
    gender = str(request.form['gender'])
    if gender=='Male':
            ge = 0
    elif gender=='Female':
                ge = 1    
    errorGender = None
    country = str(request.form['country'])
    errorCountry = None
    email =str(request.form['email'])
    errorEmail = None
    username = str(request.form['username'])
    errorUsername = None
    password = str(request.form['password'])
    confirmpassword = str(request.form['confirmpassword'])
    errorPassword = None
    
    if request.method == 'POST':
        if firstname == '':          
            errorFN = 'missing firstname'
        if lastname == '':
            errorLN = 'missing lastname'
        if month == '':
            errorMonth = 'missing month'
        if day =='':
            errorDay = 'missing day'
        if year =='':
            errorYear = 'missing year'
        if gender == '':
            errorGender = 'missing gender'
        if country =='':
            errorCountry ='missing home country'
        if email =='' or '@' not in email:
            errorEmail ='not a valid email'
        if username=='':
            errorUsername = 'missing username'
        if password=='' or confirmpassword =='':
            errorPassword = 'please fill both the password and its confirmation fields'
        if password != confirmpassword:
            errorPassword = 'mismatching passwords'
        if errorFN==None and errorLN==None and errorMonth==None and errorDay==None and errorYear==None and errorGender==None and errorCountry==None and errorEmail==None:
             date = str(request.form['year'])+'-'+str(request.form['month'])+'-'+str(request.form['day'])

             conn = oursql.connect(host='127.0.0.1', user='flight', passwd='123',
             db='bridgeit')             
             curs = conn.cursor()
             cur = curs.execute ('SELECT id FROM `country` WHERE name = \''+country+"\'")
             curs.execute ('SELECT id FROM `country` WHERE name = \''+country+"\'")
             cti = [(row[0]) for row in curs.fetchall()]
             ct = cti[0]
             print ct
             curs.close()

             conn = oursql.connect(host='127.0.0.1', user='flight', passwd='123',
             db='bridgeit')
             curs = conn.cursor()
             curs.execute ('INSERT INTO `user` (f_name, l_name, dob, gender, country_id, email, username, password) VALUES (?, ?, ?, ?, ?, ?, ?, ?)',
                           (firstname,lastname,date,ge,ct,email,username,password))
             curs.close()
             return redirect (url_for('registration'))
      
        return render_template('registration.html', firstname=firstname, errorFN=errorFN,
                           lastname=lastname, errorLN=errorLN, month=month, errorMonth=errorMonth,
                               day=day, errorDay=errorDay, year=year, errorYear=errorYear,
                               gender=gender, errorGender=errorGender, country=country, errorCountry=errorCountry,
                               email=email, errorEmail=errorEmail, username=username, errorUsername=errorUsername,
                               errorPassword=errorPassword)




if __name__ == '__main__':
    app.run()
